let arr = [8,7,6,2,9,3,4,1]

function mergeSort(arr) {
    if (arr.length < 2) return arr

    let mid = Math.floor(arr.length / 2) 
    // let leftArr = arr.slice(0, mid)
    // let rightArr = arr.slice(mid)

    let merge = function(leftArr, rightArr) {
        let resultArr = []
        while(leftArr.length && rightArr.length) {
            resultArr.push(leftArr[0] <= rightArr[0] ? leftArr.shift() : rightArr.shift())
        }
        

        return resultArr.concat(leftArr).concat(rightArr)
    }

    return merge(
        mergeSort(arr.slice(0, mid)), 
        mergeSort(arr.slice(mid))
    )
}

console.log(mergeSort(arr))